/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.world;

import cz.fi.muni.diplomka.common.Coordinates;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityResourcesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityResourcesManagerImpl;
import cz.muni.fi.diplomka.webapp.city.CityPage;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class BuildPanel extends Panel {

    private CityManager cityManager = new CityManagerImpl();
    private CityResourcesManager cityResourcesManager = new CityResourcesManagerImpl();
    private String selectedCity = "";
    private List<String> cities;

    public BuildPanel(String id, final PageParameters parameters) {
        super(id);

        if (parameters.containsKey("id") && parameters.containsKey("x") && parameters.containsKey("y")) {
            final Long playerId = Long.parseLong(parameters.getString("id"));

            final int x = Integer.parseInt(parameters.getString("x"));
            final int y = Integer.parseInt(parameters.getString("y"));

            PageParameters p = new PageParameters();
            p.add("id", playerId.toString());

            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(playerId)) {
                cities.add(c.getName());
            }

            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            p.add("cityName", selectedCity);

            final RequiredTextField<String> cityName = new RequiredTextField<>("cityName", Model.of(""));



            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            Form<?> selectCity = new Form<Void>("selectCity") {
                @Override
                protected void onSubmit() {
                    selectedCity = cities.get(Integer.parseInt(city.getValue()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", playerId.toString());
                    pageParameters.add("x", Integer.toString(x));
                    pageParameters.add("y", Integer.toString(y));
                    pageParameters.add("cityName", selectedCity);
                    setResponsePage(TilePage.class, pageParameters);
                }
            };

            add(selectCity);
            selectCity.add(city);

            Form<?> build = new Form<Void>("build") {
                @Override
                protected void onSubmit() {
                    List<ResourceType> resources = new ArrayList<>();
                    resources.add(ResourceType.Food);
                    resources.add(ResourceType.Gold);
                    resources.add(ResourceType.Stone);

                    boolean ok = true;
                    for (ResourceType resourceType : resources) {
                        if (cityResourcesManager.getResourceCount(cityManager.getCityByName(selectedCity).getId(), resourceType) < 10000) {
                            error("Not enough of " + resourceType.toString() + ". You have "
                                    + cityResourcesManager.getResourceCount(cityManager.getCityByName(selectedCity).getId(), resourceType)
                                    + " in " + selectedCity + ", but you need 10000.");
                            ok = false;
                        }
                    }
                    if (cityManager.getCityByName(cityName.getValue()).getId() != null) {
                        error("Name already taken");
                        ok = false;
                    }

                    if (ok) {
                        City city = new City();
                        city.setName(cityName.getValue());
                        city.setPlayerId(playerId);
                        Coordinates c = new Coordinates();
                        c.setX_coordinate(x);
                        c.setY_coordinate(y);
                        city.setCoordinates(c);
                        cityManager.createCity(city);
                        cityResourcesManager.lowerResource(cityManager.getCityByName(selectedCity).getId(), ResourceType.Food, 10000);
                        cityResourcesManager.lowerResource(cityManager.getCityByName(selectedCity).getId(), ResourceType.Gold, 10000);
                        cityResourcesManager.lowerResource(cityManager.getCityByName(selectedCity).getId(), ResourceType.Stone, 10000);

                        PageParameters pageParameters = new PageParameters();
                        pageParameters.add("id", playerId.toString());
                        pageParameters.add("cityName", cityName.getValue());
                        setResponsePage(CityPage.class, pageParameters);
                    }
                }
            };
            build.add(cityName);
            add(build);
        }
    }
}
